// Copyright 2013 Google Inc. All Rights Reserved.

#include <irt_syscalls.h>
#include <stdio.h>
#include <stdlib.h>

int __set_tls(void *ptr) {
  // We ask the service runtime to set the register for TLS properly
  // at the very beginning of each threads. On i686, the register is
  // %gs and the NaCl validator does not allow user code to set this
  // value.
  if (__nacl_irt_tls_init(ptr) != 0) {
    // We must not proceed the exceution when we fail to initialize
    // TLS. As stdio may not be ready now, we use pure IRT calls to
    // report the issue.
    static const char msg[] = "__nacl_irt_tls_init failed!\n";
    static const int kStderrFd = 2;
    write(kStderrFd, msg, sizeof(msg) - 1);
    exit(1);
  }
  return 0;
}
